from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import QInputDialog

from project.MachineSelfInterface import MachineSelfInterface
from project.ManMachineInterface import ManMachineInterface
from project.ui.ConfigurationPage import *
import sys


class FirstPage(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        self.ConfigurationDict = {}
        # 用于放置子窗口
        self.childLayOut = QtWidgets.QGridLayout()
        super(FirstPage, self).__init__(parent)
        self.num = 0
        self.setupUi(self)
        self.defaultConfigArgs()
        self.configArgsBind()

    # 为所有下拉框绑定事件
    def configArgsBind(self):
        # 事件处理
        self.mapcombo.activated[str].connect(self.onActivatedMapSize)
        self.channelcombo.activated[str].connect(self.onActivatedChannel)
        self.communicatecombo.activated[str].connect(self.onActivatedCommunicateNumber)
        self.disturbcombo.activated[str].connect(self.onActivatedDisturbNumber)
        self.gameModecombo.activated[str].connect(self.onActivatedGameMode)
        self.orderPrioritycombo.activated[str].connect(self.onActivatedOrderPriority)
        self.roundscombo.activated[str].connect(self.onActivatedRounds)
        self.oneRoundNumberscombo.activated[str].connect(self.onActivatedOneRoundNumbers)
        self.comStepscombo.activated[str].connect(self.onActivatedComSteps)
        self.disturbStepscombo.activated[str].connect(self.onActivatedDisturbSteps)
        self.disturbAreacombo.activated[str].connect(self.onActivatedDisturbArea)
        self.DisturbUnitInitPosCombo.activated[str].connect(self.onActivatedDisturbUnitInitPos)
        self.DistributedCombo.activated[str].connect(self.onActivatedDistributed)

        self.conformConfigBtn.clicked.connect(self.conformConfigButton)

    def defaultConfigArgs(self):

        self.ConfigurationDict['MapSize_Length'] = 25
        self.ConfigurationDict['MapSize_Height'] = 25
        self.ConfigurationDict['MapSize'] = self.mapcombo.currentText()

        self.ConfigurationDict['Number_Of_Communication'] = self.communicatecombo.currentText()
        self.ConfigurationDict['Number_Of_Disturb'] = self.disturbcombo.currentText()
        # self.ConfigurationDict['GameMode'] = self.gameModecombo.currentText()
        self.ConfigurationDict['GameMode'] = 0
        self.ConfigurationDict['Order_Priority'] = 0
        self.ConfigurationDict['Rounds'] = self.roundscombo.currentText()
        self.ConfigurationDict['OneRoundNumbers'] = self.oneRoundNumberscombo.currentText()
        self.ConfigurationDict['ComSteps'] = self.comStepscombo.currentText()
        self.ConfigurationDict['DisturbSteps'] = self.disturbStepscombo.currentText()
        self.ConfigurationDict['DisturbArea'] = self.disturbAreacombo.currentText()
        # self.ConfigurationDict['DisturbUnitInitPos'] = self.DisturbUnitInitPosCombo.currentText()
        self.ConfigurationDict['DisturbUnitInitPos'] = 0
        # self.ConfigurationDict['Distributed'] = self.DistributedCombo.currentText()
        self.ConfigurationDict['Distributed'] = 0

    #  战场配置
    def onActivatedMapSize(self, text):

        splitStr = text.split('x')
        self.ConfigurationDict['MapSize_Length'] = int(splitStr[0])
        self.ConfigurationDict['MapSize_Height'] = int(splitStr[1])

        self.ConfigurationDict['MapSize'] = text

    def onActivatedDisturbNumber(self, text):
        self.ConfigurationDict['Number_Of_Disturb'] = int(text)

    def onActivatedRounds(self, text):
        # self.ConfigurationDict['Rounds'] = int(text)

        if text == "自定义":
            text, ok = QInputDialog.getText(None, 'Input Dialog',
                                            '输入回合数:')
            if ok:
                try:
                    tmp = int(text)
                    self.ConfigurationDict["Rounds"] = tmp
                    # print(tmp)
                    self.roundscombo.insertItem(self.roundscombo.count() - 1, text)

                    self.roundscombo.setCurrentIndex(self.roundscombo.count() - 2)
                except:
                    print("GoBangGUI onActivatedRounds is error.................")
                    self.ConfigurationDict["Rounds"] = 100
                    self.roundscombo.setCurrentIndex(0)
        else:
            self.ConfigurationDict["Rounds"] = int(text)

    def onActivatedCommunicateNumber(self, text):
        self.ConfigurationDict['Number_Of_Communication'] = int(text)

    def onActivatedChannel(self, text):
        # self.ConfigurationDict['Channel_Number'] = int(text)
        try:
            if text == "自定义":
                text, ok = QInputDialog.getText(None, 'Input Dialog',
                                                '输入回合数:')
                if ok:
                    try:
                        tmp = int(text)
                        self.ConfigurationDict["Channel_Number"] = tmp
                        self.channelcombo.insertItem(self.channelcombo.count() - 1, text)

                        self.channelcombo.setCurrentIndex(self.channelcombo.count() - 2)
                    except:
                        print("GoBangGUI onActivatedChannel is error.................")
                        self.ConfigurationDict["Channel_Number"] = 10
                        self.channelcombo.setCurrentIndex(0)
            else:
                self.ConfigurationDict["Channel_Number"] = int(text)
        except:
            self.ConfigurationDict["Channel_Number"] = 10
            self.channelcombo.setCurrentIndex(0)

    #  对弈配置
    def onActivatedGameMode(self, text):
        if text == "人机对弈":
            self.ConfigurationDict['GameMode'] = 0
        else:
            self.ConfigurationDict['GameMode'] = 1

    def onActivatedOrderPriority(self, text):
        if text == "红方先":
            self.ConfigurationDict['Order_Priority'] = 0

        else:
            self.ConfigurationDict['Order_Priority'] = 1

    def onActivatedOneRoundNumbers(self, text):
        # self.ConfigurationDict['OneRoundNumbers'] = int(text)
        try:
            # print("自定义")
            if text == "自定义":
                text, ok = QInputDialog.getText(None, 'Input Dialog',
                                                '输入回合数:')
                # print(text)
                if ok:
                    tmp = int(text)
                    self.ConfigurationDict["OneRoundNumbers"] = tmp
                    self.oneRoundNumberscombo.insertItem(self.oneRoundNumberscombo.count() - 1, text)
                    self.oneRoundNumberscombo.setCurrentIndex(self.oneRoundNumberscombo.count() - 2)
            else:
                self.ConfigurationDict["OneRoundNumbers"] = int(text)
        except:
            print("GoBangGUI onActivatedOneRoundNumbers is error.................")
            self.ConfigurationDict["OneRoundNumbers"] = 5
            self.oneRoundNumberscombo.setCurrentIndex(0)

    def onActivatedComSteps(self, text):
        # self.ConfigurationDict['ComSteps'] = int(text)
        try:
            # print("自定义")
            if text == "自定义":
                text, ok = QInputDialog.getText(None, 'Input Dialog',
                                                '输入回合数:')
                # print(text)
                if ok:
                    tmp = int(text)
                    self.ConfigurationDict["ComSteps"] = tmp
                    self.comStepscombo.insertItem(self.comStepscombo.count() - 1, text)
                    self.comStepscombo.setCurrentIndex(self.comStepscombo.count() - 2)
            else:
                self.ConfigurationDict["ComSteps"] = int(text)
        except:
            print("GoBangGUI onActivatedOneRoundNumbers is error.................")
            self.ConfigurationDict["ComSteps"] = 2
            self.comStepscombo.setCurrentIndex(0)

    def onActivatedDisturbSteps(self, text):
        # self.ConfigurationDict['DisturbSteps'] = int(text)
        try:
            # print("自定义")
            if text == "自定义":
                text, ok = QInputDialog.getText(None, 'Input Dialog',
                                                '输入回合数:')
                # print(text)
                if ok:
                    tmp = int(text)
                    self.ConfigurationDict["DisturbSteps"] = tmp
                    self.disturbStepscombo.insertItem(self.disturbStepscombo.count() - 1, text)
                    self.disturbStepscombo.setCurrentIndex(self.disturbStepscombo.count() - 2)
            else:
                self.ConfigurationDict["DisturbSteps"] = int(text)
        except:
            print("GoBangGUI onActivatedOneRoundNumbers is error.................")
            self.ConfigurationDict["DisturbSteps"] = 2
            self.disturbStepscombo.setCurrentIndex(0)

    def onActivatedDisturbArea(self, text):
        try:
            # print("自定义")
            if text == "自定义":
                text, ok = QInputDialog.getText(None, 'Input Dialog',
                                                '输入回合数:')
                # print(text)
                if ok:
                    tmp = int(text)
                    self.ConfigurationDict["DisturbArea"] = tmp
                    self.disturbAreacombo.insertItem(self.disturbAreacombo.count() - 1, text)
                    self.disturbAreacombo.setCurrentIndex(self.disturbAreacombo.count() - 2)
            else:
                self.ConfigurationDict["DisturbArea"] = int(text)
        except:
            print("GoBangGUI onActivatedOneRoundNumbers is error.................")
            self.ConfigurationDict["DisturbArea"] = 2
            self.disturbStepscombo.setCurrentIndex(0)

    #  位置初始化配置
    def onActivatedDistributed(self, text):
        if text == "是":
            self.ConfigurationDict["Distributed"] = 0
        else:
            self.ConfigurationDict["Distributed"] = 1

    def onActivatedDisturbUnitInitPos(self, text):
        if text == "位于通讯单元前方":
            self.ConfigurationDict["DisturbUnitInitPos"] = 0
        elif text == "位于通讯单元后方":
            self.ConfigurationDict["DisturbUnitInitPos"] = 1
        else:  # 随机
            self.ConfigurationDict["DisturbUnitInitPos"] = 2

    # 根据参数配置信息，选择显示人机交互界面或者机器交互界面
    def conformConfigButton(self):
        print("# 显示人机交互界面")
        GameMode = self.ConfigurationDict['GameMode']
        print(GameMode)
        if int(GameMode) == 0:  # 显示人机交互界面
            # 人机交互界面

            self.InteractiveWindow = ManMachineInterface(self.ConfigurationDict)

        else:  # 显示机器交互界面
            # 机器交互界面

            self.InteractiveWindow = MachineSelfInterface(self.ConfigurationDict)

        self.childLayOut.addWidget(self.InteractiveWindow)  # 将窗口放入girdLayout中
        self.InteractiveWindow.show()  # 打开子窗口1

    #  返回所有的配置参数（字典）
    def getConfigurationDict(self):
        return self.ConfigurationDict


if __name__=="__main__":
    app = QtWidgets.QApplication(sys.argv)
    example = FirstPage()
    example.show()
    flag = app.exec_()
    print(example.ConfigurationDict)
    sys.exit(flag)
